package com.onlinelibrary.service.impl;

import com.onlinelibrary.mapper.BookMapper;
import com.onlinelibrary.mapper.BorrowMapper;
import com.onlinelibrary.mapper.HisBorrowMapper;
import com.onlinelibrary.mapper.UserMapper;
import com.onlinelibrary.pojo.Book;
import com.onlinelibrary.pojo.Borrow;
import com.onlinelibrary.pojo.User;
import com.onlinelibrary.result.Result;
import com.onlinelibrary.service.BorrowService;
import com.onlinelibrary.util.UserUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@RequiredArgsConstructor
@Service
public class BorrowServiceImpl01 implements BorrowService {

    private final BookMapper bookMapper;
    private final BorrowMapper borrowMapper;

    private final HisBorrowMapper hisBorrowMapper;

    private final UserMapper userMapper;
    private final UserUtil userUtil;
    @Override
    public Result createBorrow(Borrow borrow) {
        User user = borrowMapper.getUser(borrow);

        if (user.getName()==null|| user.getName().isEmpty()||user.getIdNumber()==null||user.getIdNumber().isEmpty()){
            return new Result(1, "请先完善姓名和身份证号信息再下单");
        }
        borrow.setCount(borrow.getBid().split(",").length);
        System.out.println(borrow);
        if (borrow.getBorrowTime()==null||borrow.getReturnTime()==null){
            return new Result(1, "请填写时间");
        }
        //现在的时间加uuid后八位
        borrow.setBorrowType(new SimpleDateFormat("yyyy-MM-dd|HH:mm:ss").format(new Date()) + UUID.randomUUID().toString().substring(23));
        //验证剩余图书是否足够
        String bid = borrow.getBid();
        String[] splitBid = bid.split(",");
        List<Book> books = userMapper.selectBooksByIsbns(Arrays.stream(splitBid).toList());
        for (Book book : books) {
            if (book.getStock()==0){
                return new Result(1, "图书"+book.getBookName()+"已借完");
            }
        }
        //前往数据库减1
        Integer i = userMapper.sortByIsbns(Arrays.stream(splitBid).toList());



        Integer result = borrowMapper.createBorrow(borrow);
        if (result > 0) {
            return new Result(10000, "申请成功");
        } else {
            return new Result(1, "申请失败");
        }
    }

    @Override
    public Result getBorrows(Borrow borrow) {
        List<Borrow> list1 = borrowMapper.getBorrows(borrow);
        List<Borrow> list2 = hisBorrowMapper.getBorrows(borrow);
        boolean b = list1.addAll(list2);
        System.out.println(b);
        return new Result(10000, "查询成功", list1);
    }

    @Override
    public Result getAllBorrows(Borrow borrow) {
        List<Borrow> allBorrows1 = borrowMapper.getAllBorrows(borrow);
        return new Result(10000, "查询成功", allBorrows1);
    }

    @Override
    public Result getAllHisBorrows(Borrow borrow) {
        List<Borrow> allBorrows2 = hisBorrowMapper.getAllBorrows(borrow);
        return new Result(10000, "查询成功", allBorrows2);
    }

    @Override
    public Result checkOk(Borrow borrow) {
        Integer i=borrowMapper.checkOk(borrow);
        if (i>0){
            return new Result(10000,"审核成功");
        }else {
            return new Result(1,"审核失败");
        }
    }

    @Override
    public Result returnBook(Borrow borrow) {
        Borrow borrowData = borrowMapper.getBorrow(borrow);
        String bid = borrowData.getBid();
        String[] splitBid = bid.split(",");
        Integer i = userMapper.addByIsbns(List.of(splitBid));
        Borrow borrow1 = borrowMapper.getBorrow(borrow);
        if (borrow1!=null){
            borrowMapper.deleteBorrow(borrow);
            borrow1.setState(4);
            hisBorrowMapper.insertHisBorrow(borrow1);
            return new Result(10000,"归还成功");
        }else {
            return new Result(1,"归还失败");
        }
    }

    @Override
    public Result getHotBooks() {
        List<Borrow> list1 = borrowMapper.list();
        List<Borrow> list2 = hisBorrowMapper.list();
        list1.addAll(list2);
        StringBuilder sb=new StringBuilder();
        for (Borrow borrow : list1) {
            String bid = borrow.getBid();
            sb.append(bid).append(",");
        }
        String[] isbns = sb.toString().split(",");
        Map<String, Integer> isbnCounts = new HashMap<>();

        for (String isbn : isbns) {
            isbnCounts.put(isbn, isbnCounts.getOrDefault(isbn, 0) + 1);
        }
        List<Book> bookList = bookMapper.list();
        for (Book book : bookList) {
            book.setStock(isbnCounts.getOrDefault(book.getIsbn(), 0));
        }
        List<Book> sortedBookList = new ArrayList<>(bookList);
        sortedBookList.sort((b1, b2) -> Integer.compare(b2.getStock(), b1.getStock()));
        return new Result(10000, "查询成功", sortedBookList);
    }
}
